﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.UserProfiles;

namespace UsersBirthdays_HireDates
{
    class theJob: Microsoft.SharePoint.Administration.SPJobDefinition
    {
        //string siteUrl = "http://localhost";

        public theJob()
            :base()
        {
        }

 

        public theJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
        }



        public theJob(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Get Users Birthdays and Hire dtaes";
        }

        public override string DisplayName
        {
            get
            {
                return "(КО) Get Users Birthdays and Hire dates";
            }
        }

        public override string Description
        {
            get
            {
                return "(КО) Заполняет списки дней рождений и недавно устроившихся.";
            }
        }

        public override void Execute(Guid contentDbId)
        {
            try
            {
                DateTime curTimePeriod = DateTime.Now.AddDays(-30);
               
                //create the object of SPSite
                //SPSite oSiteCollection = SPContext.Current.Site;
                SPSite oSiteCollection = SPContext.Current.Site;
                    //new SPSite(siteUrl);
                SPServiceContext context = SPServiceContext.GetContext(oSiteCollection);
                UserProfileManager uprofileManager = new UserProfileManager(context);

                //create the object of SPWeb with the help of SPSite object:
                SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
                oWebsiteRoot.AllowUnsafeUpdates = true;

                //create the connection to the custom List and create object of SPListItemCollection
                SPList oList = oWebsiteRoot.Lists["Birthdays"];
                SPListItemCollection listItemCOll;

               
                SPListItem oListItem = oList.Items.Add();

                foreach(UserProfile cUp in uprofileManager)
                {
                    //add the item in the User List
                    //cUp.GetProfileValueCollection().Value.ToString();
                    if(cUp["PreferredName"].Value != null && cUp["Department"].Value != null && cUp["Office"].Value != null)
                    {

                        //if(DateTime.Parse(cUp["SPS-HireDate"].Value.ToString()).Date >= curTimePeriod)
                        //{
                            oListItem["PreferredName"] = cUp["PreferredName"].Value.ToString();
                            //oListItem["SPS-JobTitle"] = cUp["SPS-JobTitle"].Value.ToString();
                            //oListItem["Department"] = cUp["Department"].Value.ToString();
                            //oListItem["Office"] = cUp["Office"].Value.ToString();
                            //oListItem["PictureUrl"] = cUp["PictureUrl"].Value.ToString();
                            //oListItem["SPS-HireDate"] = cUp["SPS-HireDate"].Value.ToString();
                            //oListItem["SPS-Birthday"] = cUp["SPS-Birthday"].Value.ToString();
                        //}
                    }
                }

                oListItem.Update();             
                            
             
            }
            catch (Exception ex)
            {
            }
        }
        }

    }
